package com.competition.login.service.impl;

import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.MD5;
import com.competition.commonutils.R;
import com.competition.login.entity.User;
import com.competition.login.entity.vo.RegisterVo;
import com.competition.login.mapper.UserMapper;
import com.competition.login.service.UserService;
import com.competition.servicebase.exceptionhandler.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public R login(User user) {
        //获取登录手机号和密码
        String password = user.getPassword();
        String email= user.getEmail();
        //手机号和密码非空判断
        if(StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            //throw new MyiException(20001,"登录失败");
            //throw new MyException(20001,"登录失败");
            return R.error().data("msg","账号或密码为空");
        }
        //判断手机号是否正确
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        User mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember == null) {//没有这个手机号
            //throw new MyException(20001,"该账号不存在");
            return R.error().data("msg","该账号不存在");
        }
        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            //throw new MyException(20001,"密码错误");
            return R.error().data("msg","密码错误");
        }
        //判断用户是否禁用
        if(mobileMember.getIsDisabled()==1) {
            //throw new MyException(20001,"亲，您已被拉黑");
            return R.error().data("msg","亲，您已被拉黑");
        }
        //登录成功
        //生成token字符串，使用jwt工具类（将用户id数据搞到token字符串中）
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return R.ok().data("token",jwtToken);
    }

    @Override
    public R register(RegisterVo registerVo) {
        System.out.println(registerVo.toString());
        String email = registerVo.getEmail();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        //判断验证码
        //判断邮箱是否重复
        QueryWrapper<User> ucenterMemberQueryWrapper=new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("email",email);
        Integer count = baseMapper.selectCount(ucenterMemberQueryWrapper);
        System.out.println(count);
        if (count>0){
            return R.error().data("msg","该邮箱已存在");
        }
        //把数据添加进数据库
        //数据添加数据库中
        User member = new User();
        member.setEmail(email);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));//密码需要加密的
        member.setIsDisabled(0);//用户不禁用
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
        return R.ok();
    }

    @Override
    public User getOpenIdMember(String openid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        User user = baseMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.selectRegisterCount(day);
    }
}
